var userService = require('../service/userService')
const JWT = require('../util/JWT')

module.exports = {
    async login(req, res) {
        var result = await userService.login(req.body)
        if (result) {
            let { _id, username, role, gender, introduction, avatarUrl } = result
            const data = { _id, username, role }
            res.header('Authorization', JWT.generate(data, '1800s'))

            res.send({
                status: "success",
                data: {
                    ...data,
                    gender,
                    introduction,
                    avatarUrl
                }
            })
        } else {
            res.status(401).send({
                status: "用户名或密码错误",
            })
        }
    },
    async add(req, res) {
        var result = await userService.add(req.body)
        res.send(result)
    },
    async getList(req, res) {
        var result = await userService.getList()
        res.send(result)
    },
    async update(req, res) {
        var result = await userService.update(req.params.id, req.body)
        res.send(result)
    },
    async delete(req, res) {
        var result = await userService.delete(req.body)
        res.send(result)
    }
}